package com.xyh.flowershop.controller;

import com.xyh.flowershop.annotations.LoggingRecord;
import com.xyh.flowershop.constant.RequestBaseConstant;
import com.xyh.flowershop.service.FileService;
import com.xyh.flowershop.service.SwipersService;
import com.xyh.flowershop.vo.req.swiper.SwiperEditReqVo;
import com.xyh.flowershop.vo.req.swiper.SwiperPageReqVo;
import com.xyh.flowershop.vo.resp.common.ResultRespVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 小约翰可汗
 * @className SwipersController
 * @createTime 2024/4/514:04
 */
@Slf4j
@LoggingRecord(excludeMethods = {"uploadFile"})
@CrossOrigin(origins = "*", maxAge = 3600)
@Tag(name = "轮播图信息接口")
@RequestMapping("/swipers/")
@RestController
public class SwipersController {

    private final SwipersService swipersService;

    private final FileService fileService;

    public SwipersController(SwipersService swipersService, FileService fileService) {
        this.swipersService = swipersService;
        this.fileService = fileService;
    }

    @Operation(description = "编辑/新增轮播图信息")
    @PostMapping("editOrAdd")
    public ResultRespVo editOrAddSwiper(@RequestBody SwiperEditReqVo vo){
        Boolean flag;
        if(vo.getId() == null){
            flag = swipersService.addSwiper(vo);
        }else {
            flag = swipersService.editSwiper(vo);
        }
        return flag ? ResultRespVo.success() : ResultRespVo.error();
    }

    @Operation(description = "删除轮播图信息")
    @DeleteMapping("delete/{id}")
    public ResultRespVo deleteSwiper(@PathVariable Long id){
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        Boolean flag = swipersService.deleteSwiper(ids);
        return flag ? ResultRespVo.success() : ResultRespVo.error("该数据已被删除，请刷新重试");
    }

    @Operation(description = "批量删除轮播图信息")
    @DeleteMapping("delete/batch")
    public ResultRespVo deleteSwiperBatch(@RequestBody List<Long> ids){
        Boolean flag = swipersService.deleteSwiper(ids);
        return flag ? ResultRespVo.success() : ResultRespVo.error();
    }


    @Operation(description = "分页查找轮播图数据")
    @PostMapping("app/find/page")
    public ResultRespVo findPageSwipers(@RequestBody SwiperPageReqVo vo){

        return ResultRespVo.success(swipersService.getPageSwiper(vo));
    }

    @Operation(description = "根据编号查找信息")
    @GetMapping("find/{id}")
    public ResultRespVo findSwiper(@PathVariable Long id){
        return ResultRespVo.success(swipersService.getSwiper(id));
    }

    @Operation(description = "文件上传")
    @PostMapping("upload")
    public ResultRespVo uploadFile(@RequestBody MultipartFile file, HttpServletRequest request){
        String path = RequestBaseConstant.BASE_URL+"/img/swipers/"+fileService.uploadFile(file, request.getRequestURI());
        return ResultRespVo.success(path);
    }

    @Operation(description = "停用/启用轮播图")
    @GetMapping("change/status/{id}/{status}")
    public  ResultRespVo changeStatus(@PathVariable Long id, @PathVariable Integer status){
        return swipersService.changeStatus(id,status) ? ResultRespVo.success() : ResultRespVo.error();
    }
}
